
# 自动生成的后端过滤逻辑

def extract_filter_parameters():
    """提取所有过滤器参数"""
    symbols = request.args.getlist('symbols')
    directions = request.args.getlist('directions')
    trade_types = request.args.getlist('trade_types')
    sides = request.args.getlist('sides')
    sessions = request.args.getlist('sessions')
    profit_status = request.args.getlist('profit_status')
    nth_trade_min = request.args.get('nth_trade_min')
    nth_trade_max = request.args.get('nth_trade_max')
    nth_trade = request.args.get('nth_trade')
    return {"symbols": symbols, "directions": directions, "trade_types": trade_types, "sides": sides, "sessions": sessions, "profit_status": profit_status, "nth_trade_min": nth_trade_min, "nth_trade_max": nth_trade_max, "nth_trade": nth_trade}

def apply_all_filters(query, filters):
    """应用所有过滤器"""
    from sqlalchemy import or_, func

    # 选择交易品种
    if symbols:
        if false:
            query = query.filter(Trade.symbol.in_(symbols))
        else:
            conditions = [func.lower(Trade.symbol) == func.lower(value) for value in symbols]
            query = query.filter(or_(*conditions))

    # 选择交易方向
    if directions:
        if false:
            query = query.filter(Trade.direction.in_(directions))
        else:
            conditions = [func.lower(Trade.direction) == func.lower(value) for value in directions]
            query = query.filter(or_(*conditions))

    # 选择交易类型
    if trade_types:
        if false:
            query = query.filter(Trade.trade_type.in_(trade_types))
        else:
            conditions = [func.lower(Trade.trade_type) == func.lower(value) for value in trade_types]
            query = query.filter(or_(*conditions))

    # 选择交易Side
    if sides:
        if false:
            query = query.filter(Trade.side.in_(sides))
        else:
            conditions = [func.lower(Trade.side) == func.lower(value) for value in sides]
            query = query.filter(or_(*conditions))

    # 选择交易时段
    if sessions:
        if false:
            query = query.filter(Trade.session.in_(sessions))
        else:
            conditions = [func.lower(Trade.session) == func.lower(value) for value in sessions]
            query = query.filter(or_(*conditions))

    # 选择盈亏状态
    if profit_status:
        profit_conditions = []
        if "profitable" in profit_status:
            profit_conditions.append(Trade.net_profit > 0)
        if "losing" in profit_status:
            profit_conditions.append(Trade.net_profit <= 0)
        if profit_conditions:
            query = query.filter(or_(*profit_conditions))

    # 
    if nth_trade_min:
        try:
            min_val = float(nth_trade_min)
            query = query.filter(Trade.nth_trade >= min_val)
        except ValueError:
            pass
    if nth_trade_max:
        try:
            max_val = float(nth_trade_max)
            query = query.filter(Trade.nth_trade <= max_val)
        except ValueError:
            pass

    # 当天第几笔
    if nth_trade:
        try:
            val = float(nth_trade)
            query = query.filter(Trade.nth_trade == val)
        except ValueError:
            pass
    return query